<?php

use yii\db\Migration;

/**
 * Handles the creation of table `{{%project_donation}}`.
 * Has foreign keys to the tables:
 *
 * - `{{%donor}}`
 * - `{{%project}}`
 */
class m230706_055801_create_project_donation_table extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $this->createTable('{{%project_donation}}', [
            'id' => $this->primaryKey(),
            'donor_id' => $this->integer()->notNull(),
            'price' => $this->money()->notNull(),
            'is_direct_donation' => $this->boolean()->defaultValue(0)->notNull(),
            'registry_number' => $this->integer(),
            'own_number' => $this->string(),
            'date_sign' => $this->date(),
            'date_realize' => $this->date(),
            'subject' => $this->string(512),
            'filename' => $this->string(),
            'project_id' => $this->integer()->notNull(),
        ]);

        // creates index for column `donor_id`
        $this->createIndex(
            '{{%idx-project_donation-donor_id}}',
            '{{%project_donation}}',
            'donor_id'
        );

        // add foreign key for table `{{%donor}}`
        $this->addForeignKey(
            '{{%fk-project_donation-donor_id}}',
            '{{%project_donation}}',
            'donor_id',
            '{{%donor}}',
            'id',
            'RESTRICT'
        );

        // creates index for column `project_id`
        $this->createIndex(
            '{{%idx-project_donation-project_id}}',
            '{{%project_donation}}',
            'project_id'
        );

        // add foreign key for table `{{%project}}`
        $this->addForeignKey(
            '{{%fk-project_donation-project_id}}',
            '{{%project_donation}}',
            'project_id',
            '{{%project}}',
            'id',
            'RESTRICT'
        );
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        // drops foreign key for table `{{%donor}}`
        $this->dropForeignKey(
            '{{%fk-project_donation-donor_id}}',
            '{{%project_donation}}'
        );

        // drops index for column `donor_id`
        $this->dropIndex(
            '{{%idx-project_donation-donor_id}}',
            '{{%project_donation}}'
        );

        // drops foreign key for table `{{%project}}`
        $this->dropForeignKey(
            '{{%fk-project_donation-project_id}}',
            '{{%project_donation}}'
        );

        // drops index for column `project_id`
        $this->dropIndex(
            '{{%idx-project_donation-project_id}}',
            '{{%project_donation}}'
        );

        $this->dropTable('{{%project_donation}}');
    }
}
